package cn.cslg.edu.weather.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.cslg.edu.weather.R;
import cn.cslg.edu.weather.bean.WeatherResponse;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder> {

    private Context mContext;
    private List<WeatherResponse.weatherBean> mWeatherBeanList;

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView itemStation;
        TextView itemAqi;
        TextView itemPollution;

        public ViewHolder(View itemView) {
            super(itemView);
            itemStation = itemView.findViewById(R.id.item_station_name);
            itemAqi = itemView.findViewById(R.id.item_aqi);
            itemPollution = itemView.findViewById(R.id.item_pollution);
        }
    }

    public WeatherAdapter(List<WeatherResponse.weatherBean> weatherBeanList) {
        mWeatherBeanList = weatherBeanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.recyer_item, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.itemPollution.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                Toast.makeText(mContext, position, Toast.LENGTH_SHORT).show();
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeatherResponse.weatherBean weatherBean = mWeatherBeanList.get(position);
        holder.itemAqi.setText(String.valueOf(weatherBean.getAqi()));
        holder.itemStation.setText(weatherBean.getPosition_name());
        holder.itemPollution.setText(weatherBean.getPrimary_pollutant());
    }

    @Override
    public int getItemCount() {
        return mWeatherBeanList.size() - 1;
    }
}
